<?php
declare(strict_types=1);

namespace Modules\Inventory\Http\Controllers;

use Catch\Base\CatchController as Controller;
use Modules\Inventory\Models\InventoryPlan;
use Illuminate\Http\Request;
use Modules\Inventory\Repository\InventoryOrderRepository;use Modules\Inventory\Repository\InventoryPlanDiffRepository;use Modules\Inventory\Repository\InventoryPlanRepository;
use Modules\Inventory\Http\Requests\InventoryPlanRequest;

/**
 * 库存盘点方案相关
 * Class InventoryPlanController
 * @package Modules\Inventory\Http\Controllers
 */
class InventoryPlanController extends Controller
{
    public function __construct(
        protected readonly InventoryPlan $model
    ){}

    /**
     * @param Request $request
     * @return mixed
     */
    public function index(Request $request): mixed
    {
        $list = InventoryPlanRepository::getList($request->all());
        foreach ($list['data'] as &$val){

            $val['allow_new_material']     = $val['allow_new_material'] == 1 ? true : false;
            $val['no_check_order']         = $val['no_check_order'] == 1 ? true : false;
            $val['create_adjust_order']    = $val['create_adjust_order'] == 1 ? true : false;
            $val['wms_range']              = json_decode($val['wms_range'],true);
            $val['inventory_eadline']      = $val['inventory_eadline'] == 0 ? '即时库存' : date('Y-m-d H:i:s',$val['inventory_eadline']);

        }
        return $list;
    }

    /**
     * 保存盘点方案
     * @param Request $request
     * @return mixed
     */
    public function store(InventoryPlanRequest $request)
    {
        return InventoryPlanRepository::storeBy($request->post());
    }

    /**
     * 盘点方案审核
     * @param Request $request
     */
    public function confirm($planId,Request $request){
        return InventoryPlanRepository::confirm($planId);
    }

    /**
     * @param $id
     * @return mixed
     */
    public function show($id)
    {
        $plan = $this->model->firstBy($id);
        if($plan){
            $plan = $plan->toArray();

            $plan['allow_new_material']     = $plan['allow_new_material'] == 1 ? true : false;
            $plan['no_check_order']         = $plan['no_check_order'] == 1 ? true : false;
            $plan['create_adjust_order']    = $plan['create_adjust_order'] == 1 ? true : false;
            $plan['wms_range']              = json_decode($plan['wms_range'],true);
            $plan['inventory_eadline']      = $plan['inventory_eadline'] == 0 ? 0 : date('Y-m-d H:i:s',$plan['inventory_eadline']);
        }

        return $plan;
    }

    /**
     * @param Request $request
     * @param $id
     * @return mixed
     */
    public function update($id, Request $request)
    {
        return $this->model->updateBy($id, $request->all());
    }

    /**
     * @param $id
     * @return mixed
     */
    public function destroy($id)
    {
        return $this->model->deleteBy($id);
    }

    /**
     * 按盘点方案审核盘点单
     * @param Request $request
     */
    public function inventoryOrderVerify($planId,Request $request){
        return InventoryOrderRepository::verify($planId);
    }

    /**
     * 盘点方案差异汇总查询
     * @param Request $request
     * @return mixed
     */
    public function getDiffGroupBy(Request $request){
        return InventoryPlanDiffRepository::getList($request->all());
    }
}
